package zhengyanze.com.aipoemmax.guide.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.activity.PoemDetilsActivity;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;
import zhengyanze.com.aipoemmax.guide.utils.TypefaceUtil;

/**
 * 译文
 * Created by MrZheng on 2017/7/22.
 */

public class Four3Fragment extends Fragment {
    @BindView(R.id.tv_author)
    TextView tvAuthor;
    @BindView(R.id.tv_content)
    TextView tvContent;
    Unbinder unbinder;
    private PoemBean poemBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_four, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        poemBean = bundle.getParcelable(PoemDetilsActivity.POEM_FRAGMENT_KEY);
        tvAuthor.setVisibility(View.GONE);
        TypefaceUtil util = new TypefaceUtil(getContext(), Constant.TTF.TTF_MAIN_TYPE);
        util.setTypeface(tvContent, false);
        initDatas();
    }

    public void initDatas() {
        //加载数据,异步进行
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect(poemBean.getLink()).get();
                    Element main3 = document.getElementsByClass("main3").first();

                    Element left = main3.getElementsByClass("left").first();
//                    Log.e(TAG, "run: "+left.toString() );

                    final Element contyishang = left.getElementsByClass("contyishang").last();

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (contyishang != null) {
                                tvContent.setText(Html.fromHtml(contyishang.toString().replaceAll("佚名","")));
                            }else{
                                Toast.makeText(getContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                } catch (
                        IOException e)
                {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
